package com.crazyhat.crazyhat.service.impl;

import java.time.LocalDateTime;
import java.util.Map;

import com.crazyhat.crazyhat.entity.Club;
import com.crazyhat.crazyhat.mapper.ClubMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazyhat.crazyhat.entity.Footprint;
import com.crazyhat.crazyhat.mapper.FootprintMapper;
import com.crazyhat.crazyhat.service.IFootprintService;
import com.crazyhat.crazyhat.utils.ThreadLocalUtil;

@Service
public class FootprintServiceImpl extends ServiceImpl<FootprintMapper, Footprint> implements IFootprintService {


    @Autowired
    private FootprintMapper footprintMapper;

    @Autowired
    private ClubMapper clubMapper;
    public Page<Footprint> getPersonalFootprint(int pageNum, int pageSize) {

        // 获取当前用户ID
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer studentId = (Integer) map.get("studentId");
        QueryWrapper<Footprint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", studentId)
                .orderByDesc("browse_time");

        Page<Footprint> page = new Page<>(pageNum, pageSize);
        // 分页查询
        return footprintMapper.selectPage(page, queryWrapper);
    }
    @Override
    public void addFootprint(int clubId) {
        Club club=clubMapper.selectById(clubId);
        if(club==null){
            return;
        }
        Footprint footPrint=new Footprint();
        footPrint.setBrowseTime(LocalDateTime.now());
        footPrint.setClubId(clubId);
        footPrint.setClubName(club.getClubName());

        // 获取当前用户ID
        Map<String, Object> map = ThreadLocalUtil.get();

        if(map==null){
            footprintMapper.insert(footPrint);
            return;
        }

        Integer studentId = (Integer) map.get("studentId");
        footPrint.setUserId(studentId);
        footprintMapper.insert(footPrint);
    }
}
